package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.ArticleInfoDto;
import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements IApBehaviorEntryService {

    @Override
    public ApBehaviorEntry getEntry(EntryDto dto) {
        // 如果userId不为空,根据userId查询
        if (dto.getUserId() != null) {
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();
            query.eq(ApBehaviorEntry::getType, 1);
            query.eq(ApBehaviorEntry::getEntryId, dto.getUserId().toString());
            ApBehaviorEntry one = this.getOne(query);
            if (one != null) {
                return one;
            } else {
                // 第一次访问,新增行为实体并返回
                ApBehaviorEntry entry = new ApBehaviorEntry();
                entry.setType(1);
                entry.setEntryId(dto.getUserId().toString());
                entry.setCreatedTime(new Date());
                this.save(entry);
                return entry;
            }
        }
        // 如果设备id不为空,根据设备id查询
        if (!StringUtils.isEmpty(dto.getEquipmentId())) {
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();
            query.eq(ApBehaviorEntry::getType, 0);
            query.eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId());
            ApBehaviorEntry one = this.getOne(query);
            if (one != null) {
                return one;
            } else {
                // 第一次访问,新增行为实体并返回
                ApBehaviorEntry entry = new ApBehaviorEntry();
                entry.setType(0);
                entry.setEntryId(dto.getEquipmentId());
                entry.setCreatedTime(new Date());
                this.save(entry);
                return entry;
            }
        }
        return null;
    }

    @Autowired
    private IApFollowBehaviorService apFollowBehaviorService;
    @Autowired
    private IApUnlikesBehaviorService apUnlikesBehaviorService;
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 查询标记：喜欢 不喜欢 收藏 关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<BehaviorDto> loadArticleBehavior(ArticleInfoDto dto) {
        //封装返回对象
        BehaviorDto behaviorDto = new BehaviorDto();
        //关注
        Query queryFollow=new Query(Criteria
                .where("userId").is(dto.getUserId())//当前用户id
                .and("followId").is(dto.getFollowId())//关注用户id
                .and("equipmentId").is(dto.getEquipmentId())
                .and("operation").is(0)//关注状态
        );
        ApFollowBehavior one = mongoTemplate.findOne(queryFollow, ApFollowBehavior.class);
        //查询到关注信息设置Isfollow关注字段为true
        if(one!=null){
            behaviorDto.setIsfollow(true);
        }
        //不喜欢
        LambdaQueryWrapper<ApUnlikesBehavior> queryUnlike=new LambdaQueryWrapper<>();
        queryUnlike.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        queryUnlike.eq(ApUnlikesBehavior::getEntryId,dto.getUserId());
        ApUnlikesBehavior unlikeOne = apUnlikesBehaviorService.getOne(queryUnlike);
        //查到不喜欢数据Isunlike不喜欢字段
        if (unlikeOne!=null){
            behaviorDto.setIsunlike(true);
        }
        //点赞

        return ResponseResult.okResult(behaviorDto);
    }
}
